package net.grandnature.android.morse.decode.example2;

import java.util.Arrays;

public class ValueHolder {
	private int size;
	private double[] values;
	private static final double BLANK = Double.MIN_VALUE;
	private int position = 0;
	public ValueHolder(int size) {
		this.size = size;
		values = new double[size];
		clear();
	}
	public void clear() {
		Arrays.fill(values, BLANK);
	}
	// 一周したらtrue
	public boolean add(double value) {
		values[position] = value;
		if(size -1 == position) {
			position = 0;
			return true;
		} 
		position ++;
		return false;
	}
	public boolean addAbs(double value) {
		return add(Math.abs(value));
	}
	// めじあんを得る
	public double getMedian() {
		double[] tmp = values.clone();
		Arrays.sort(tmp);
		int len = tmp.length;
		int first = 0;
		for (int i = 0; i < len; i++) {
			first = i;
			if(tmp[i] != BLANK) break;
		}
		return tmp[(len - first) / 2 + first];
	}
	// 量を得る。加算してるだけ
	public double getQuantity() {
		double quan = 0;
		int len = values.length;
		for (int i = 0; i < len; i++) {
			if(values[i] == BLANK) break;
			quan += Math.abs(values[i]);
		}
		return quan;
	}
}

